package com.yiqi.lottery.feature.documentary.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import butterknife.BindView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.luck.picture.lib.entity.EventEntity;
import com.luck.picture.lib.rxbus2.RxBus;
import com.luck.picture.lib.rxbus2.Subscribe;
import com.luck.picture.lib.rxbus2.ThreadMode;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.entity.bo.PageData;
import com.suozhang.framework.framework.BaseFragment;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.lottery.R;
import com.yiqi.lottery.entity.bo.HallBo;
import com.yiqi.lottery.feature.documentary.contract.DocumentaryContract;
import com.yiqi.lottery.feature.documentary.dependencies.hall.DaggerDocumentaryComponent;
import com.yiqi.lottery.feature.documentary.dependencies.hall.DocumentaryPresenterModule;
import com.yiqi.lottery.feature.documentary.view.HallFragment;
import com.yiqi.lottery.feature.documentary.view.adapter.DocumentaryAdapter;
import com.yiqi.lottery.feature.documentary.view.adapter.LotteryDetailActivity;

import javax.inject.Inject;

import static com.yiqi.lottery.feature.documentary.view.DocumentaryFragment.BET_TYPE_FLAG;

public class ListFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener,
        BaseQuickAdapter.OnItemChildClickListener, DocumentaryContract.View,
        BaseQuickAdapter.RequestLoadMoreListener, OnRefreshListener {

    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    private DocumentaryAdapter mAdapter;
    private EmptyView mEmptyView;

    @Inject
    DocumentaryContract.Presenter mPresenter;
    PageData<HallBo> mData;
    private int pageSize = 10;
    private int mCurrentCounter = 0;
    private int pageIndex = 0;
    private boolean isErr;
    private boolean mLoadMoreEndGone = false;
    private Integer mBetType = null;
    protected int mTab = 3;

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_hall;
    }

    @Override
    protected void initInjector() {
        DaggerDocumentaryComponent.builder().documentaryPresenterModule(new DocumentaryPresenterModule(this)).build().inject(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        initData();
        if (!RxBus.getDefault().isRegistered(this)) {
            RxBus.getDefault().register(this);
        }
    }

    @Override
    protected void initView() {
        initAdapter();
    }

    @Override
    protected void initData() {
        pageIndex = 0;
        if (mPresenter != null)
            mPresenter.getHallList(null,pageIndex, mBetType, mTab);
    }

    @Override
    protected void initEvent() {

    }

    /**
     * EventBus 3.0 回调
     *
     * @param obj
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventBus(EventEntity obj) {
        switch (obj.what) {
            case BET_TYPE_FLAG:
                switch (obj.position) {
                    case 0:
                        mBetType = null;
                        break;
                    case 1:
                        mBetType = 4;
                        break;
                    case 2:
                        mBetType = 5;
                        break;
                }

                break;

        }

        initData();
    }


    private void initAdapter() {
        mAdapter = new DocumentaryAdapter();
        mEmptyView = new EmptyView(mRecyclerview);
        mRecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter.bindToRecyclerView(mRecyclerview);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnItemChildClickListener(this);
        mRefreshLayout.setEnableLoadMore(false);
        mRefreshLayout.setOnRefreshListener(this);
        mAdapter.setOnLoadMoreListener(this);

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(getActivity(), LotteryDetailActivity.class);
        intent.putExtra("id", mData.getList().get(position).getOrder_id());
        intent.putExtra("user_id",  mData.getList().get(position).getUser_info().getUser_id());
        startActivity(intent);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.btn_documentary_again:
                ShareBottomDialog shareBottomDialog = new ShareBottomDialog(getActivity(),mAdapter.getData().get(position));
                shareBottomDialog.show();
                break;
            case R.id.btn_to_personalinfo:
                Intent intent = new Intent(getActivity(), PersonalInfoActivity.class);
                intent.putExtra("id", mData.getList().get(position).getUser_info().getUser_id());
                startActivity(intent);
                break;
        }
    }

    @Override
    public void showHallListSuccess(PageData<HallBo> hallBoPageData) {
        if (mRefreshLayout != null) mRefreshLayout.finishRefresh();
        mData = hallBoPageData;
        mAdapter.setNewData(mData.getList());
        if (mAdapter.getData().size() < pageSize) {
            mAdapter.loadMoreEnd();
        }
        isErr = false;
        mCurrentCounter = pageSize;
        mAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showHallListError(String ex) {
        if (mRefreshLayout != null) mRefreshLayout.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getErrorView(ex));
        mAdapter.setNewData(null);
    }

    @Override
    public void showHallListMoreSuccess(PageData<HallBo> hallBoPageData) {
        isErr = false;
        mAdapter.addData(hallBoPageData.getList());
        mCurrentCounter = mAdapter.getData().size();
        mAdapter.loadMoreComplete();
    }

    @Override
    public void showHallListMoreError(String ex) {
        isErr = true;
        mAdapter.loadMoreFail();
    }

    @Override
    public void showHallListEmpty() {
        if (mRefreshLayout != null) mRefreshLayout.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getEmptyView());
        mAdapter.setNewData(null);
    }

    @Override
    public void onLoadMoreRequested() {

        if (mAdapter.getData().size() < pageSize) {
            mAdapter.loadMoreEnd(true);
        } else {

            if (mCurrentCounter >= mData.getTotal()) {
                mAdapter.loadMoreEnd(mLoadMoreEndGone);
            } else {
                if (!isErr) {
                    pageIndex++;
                    mPresenter.getHallMoreList(null,pageIndex, mBetType, mTab);

                }
            }

        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        initData();
    }

    @Override
    public void onStop() {
        super.onStop();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (RxBus.getDefault().isRegistered(this)) {
            RxBus.getDefault().unregister(this);
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser) {
            initData();
        }
        super.setUserVisibleHint(isVisibleToUser);
    }
}
